//
//  PasswordViewController.m
//  bitcoin
//
//  Created by caiyajun on 2018/4/27.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "PasswordViewController.h"
#import "HWDownSelectedView.h"
#import "LCGrayButton.h"

@interface PasswordViewController ()
@property (weak, nonatomic) IBOutlet UIButton *sureButton;
@property (weak, nonatomic) IBOutlet HWDownSelectedView *areaCodeView;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *messageTF;
@property (weak, nonatomic) IBOutlet UITextField *otherPasswordTF;
@property (weak, nonatomic) IBOutlet UIButton *eyeBtn;
- (IBAction)changeEyeAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet LCGrayButton *sendCodeBtn;

@end

@implementation PasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrowleft-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
//    self.navigationItem.leftBarButtonItem = back;
    [_phoneTF setValue:[UIColor colorWithHexString:@"#616372"] forKeyPath:@"_placeholderLabel.textColor"];
    [_messageTF setValue:[UIColor colorWithHexString:@"#616372"] forKeyPath:@"_placeholderLabel.textColor"];
    [_otherPasswordTF setValue:[UIColor colorWithHexString:@"#616372"] forKeyPath:@"_placeholderLabel.textColor"];
    [_eyeBtn setImage:[UIImage imageNamed:@"open-icon"] forState:UIControlStateNormal];
    [_eyeBtn setImage:[UIImage imageNamed:@"close-icon"] forState:UIControlStateSelected];
    _areaCodeView.placeholder = @"0086";
    _areaCodeView.textColor = [UIColor colorWithHexString:@"#999999"];
    _areaCodeView.font = [UIFont boldSystemFontOfSize:14];
    _areaCodeView.listArray = @[@"0086", @"0087", @"0088", @"0089"];
    [self.sendCodeBtn addTarget: self action:@selector(startTime) forControlEvents:UIControlEventTouchUpInside];
    
    

    [self.sureButton setBackgroundImage:[UIImage imageWithColor:RGBColor(86, 54, 157)] forState:(UIControlStateHighlighted)];

//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
//    [self.view addGestureRecognizer:tap];
    // Do any additional setup after loading the view.
}
//- (void)hideKeyboard{
//    [self.view endEditing:YES];
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (void)backAction:(UIBarButtonItem *)sender {
//    [self.navigationController popViewControllerAnimated:YES];
//}

- (IBAction)changeEyeAction:(UIButton *)sender {
    self.otherPasswordTF.enabled = NO;
    self.otherPasswordTF.secureTextEntry = !sender.selected;
    sender.selected = !sender.selected;
    self.otherPasswordTF.enabled = YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [_areaCodeView close];
    
}
-(void)startTime{
    
    __block int timeout = 59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.sendCodeBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
                self.sendCodeBtn.enabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.sendCodeBtn setTitle:[NSString stringWithFormat:@"重新获取%@秒",strTime] forState:UIControlStateNormal];
                self.sendCodeBtn.enabled = NO;
                
            });
            timeout--;
            
        }
    });
    dispatch_resume(_timer);
    DebugLog(@"我点击了.... ");
    NSDictionary *param = @{@"mobile" : @"15152652212"};
    [LSRequestManager postRequestWithURL:HUOBI(@"api/auth/sendMobileYzm")
                                  params:param
                                 success:^(NSDictionary *responseData) {
                                     if ([responseData[@"ret"] integerValue ] == 1) {
                                         [MBProgressHUD showError:responseData[@"msg"]];
                                     } else {
                                         [MBProgressHUD showError:responseData[@"msg"]];
                                     }
                                 }
                                 failure:^(NSError *error) {
                                     
                                 }
                                 showHUD:NO];

    
}


- (IBAction)clickAction {

}
















@end
